package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "菜品管理")
@RequestMapping("/admin/dish")
@Slf4j
@RestController
public class DishController {
    @Autowired
    private DishService dishService;

    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @ApiOperation("新增菜品")
    @PostMapping
    public Result<String> addDish(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品：{}", dishDTO);
        dishService.addDish(dishDTO);
        return Result.success();
    }

    /**
     * 分页查询菜品列表
     * 这里不需要 @RequestBody注解，因为是地址栏传参，会自动封装
     * @param dishPageQueryDTO
     * @return
     */
    @ApiOperation("分页查询菜品列表")
    @GetMapping("/page")
    public Result<PageResult> queryDishList(DishPageQueryDTO dishPageQueryDTO) {
        log.info("分页查询菜品列表：{}", dishPageQueryDTO);
        PageResult pageResult = dishService.queryDishList(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 删除菜品
     * 需要加入注解 @RequestParam 封装List集合
     * @param ids
     * @return
     */
    @ApiOperation("删除菜品")
    @DeleteMapping
    public Result<String> deleteDish(@RequestParam List<Long> ids) {
        log.info("删除菜品：{}", ids);
        dishService.deleteDish(ids);
        return Result.success();
    }

    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    @ApiOperation("根据id查询菜品")
    @GetMapping("/{id}")
    public Result<DishVO> getDishById(@PathVariable Long id) {
        log.info("根据id查询菜品：{}", id);
        DishVO dishVO = dishService.getDishById(id);
        return Result.success(dishVO);
    }

    /**
     * 更新菜品
     * @param dishDTO
     * @return
     */
    @ApiOperation("更新菜品")
    @PutMapping
    public Result<String> updateDish(@RequestBody DishDTO dishDTO) {
        log.info("更新菜品：{}", dishDTO);
        dishService.updateDish(dishDTO);
        return Result.success();
    }

    /**
     * 设置菜品状态
     * @param status
     * @param id
     */
    @ApiOperation("设置菜品状态")
    @PostMapping("/status/{status}")
    public Result<String> setDishStatus(@PathVariable Integer status, Long id) {
        log.info("设置菜品状态：{}，id：{}", status, id);
        dishService.setDishStatus(status, id);
        return Result.success();
    }

    /**
     * 根据分类id查询菜品列表
     * @param categoryId
     * @return
     */
    @ApiOperation("根据分类id查询菜品列表")
    @GetMapping("/list")
    public Result<List<Dish>> queryDishByCategoryId(Long categoryId) {
        log.info("根据分类id查询菜品列表：{}", categoryId);
        return Result.success(dishService.queryDishByCategoryId(categoryId));
    }
}
